package com.gome.ocean.service.authority.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.gome.ocean.common.constants.Constants;
import com.gome.ocean.common.exception.ProjectServiceException;
import com.gome.ocean.dao.authority.ProjectDAO;
import com.gome.ocean.dao.authority.UserDAO;
import com.gome.ocean.dao.model.authority.ProjectBO;
import com.gome.ocean.service.authority.ProjectService;

/**
 * Created by liuqingxia on 2016/3/1.
 */
@Service("projectService")
@Transactional("transactionManager")
public class ProjectServiceImpl implements ProjectService {

    private static final Logger LOG = LoggerFactory.getLogger(ProjectServiceImpl.class);
    @Resource(name = "projectDAO")
    private ProjectDAO          projectDAO;
    @Autowired
    private UserDAO             userDAO;

    @Override
    public boolean insertProject(ProjectBO project) throws ProjectServiceException {
        if (!isQualifiedProject(project)) {
            throw new ProjectServiceException("Project属性未设置完备，请检查后重新设置");
        }
        String projectName = project.getProjectName();
        if (findProjectByName(projectName) != null) {
            throw new ProjectServiceException("该项目组已经存在，请重新给项目组起名");
        }
        String projectUser = project.getProjectUser();
        String[] projectUserArr = projectUser.split(Constants.REG_CN_OR_EN_COMMA);
        for (String userEmail : projectUserArr) {
            try {
                if (userDAO.findUserByEmail(userEmail) == null) {
                    throw new ProjectServiceException("该用户(" + userEmail + ")不存在，请先创建用户");
                }
            } catch (Exception e) {
                LOG.error(e.getMessage(), e);
                throw new ProjectServiceException(e.getMessage());
            }
        }
        try {
            if (projectDAO.insertProject(project) > 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            throw new ProjectServiceException(e.getMessage());
        }
    }

    @Override
    public boolean removeProjectById(Long id) throws ProjectServiceException {
        if (projectDAO.findProjectById(id) == null) {
            throw new ProjectServiceException("数据库中不存在此项目组记录，id=" + id);
        }
        try {
            if (projectDAO.removeProjectById(id) > 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            throw new ProjectServiceException(e.getMessage());
        }
    }

    @Override
    public boolean updateProject(ProjectBO project) throws ProjectServiceException {
        if (!isQualifiedProject(project)) {
            throw new ProjectServiceException("Project属性未设置完备，请检查后重新设置");
        }
        String projectName = project.getProjectName();
        if (projectDAO.findProjectByName(projectName) == null) {
            throw new ProjectServiceException("数据库中无此项目组记录，projectName=" + projectName);
        }
        try {
            if (projectDAO.updateProject(project) > 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            throw new ProjectServiceException(e.getMessage());
        }
    }

    @Override
    public ProjectBO findProjectById(Long id) throws ProjectServiceException {
        ProjectBO project = null;
        try {
            project = projectDAO.findProjectById(id);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            throw new ProjectServiceException(e.getMessage());
        }
        return project;
    }

    @Override
    public Page<ProjectBO> findAllProjects(int pageNum, int pageSize) throws ProjectServiceException {
        if (pageNum <= 0) {
            pageNum = Constants.DEFAULT_PAGE_NUM;
        }
        if (pageSize <= 0) {
            pageSize = Constants.DEFAULT_PAGE_SIZE;
        }
        Page<ProjectBO> projects = null;
        try {
            projects = (Page<ProjectBO>) projectDAO.findAllProjects(new RowBounds(pageNum, pageSize));
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            throw new ProjectServiceException(e.getMessage());
        }
        return projects;
    }

    @Override
    public ProjectBO findProjectByName(String projectName) {
        return projectDAO.findProjectByName(projectName);
    }

    @Override
    public List<ProjectBO> findProjectByUser(String userEmail) throws ProjectServiceException {
        List<ProjectBO> projects = null;
        try {
            projects = projectDAO.findProjectByUser(userEmail);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            throw new ProjectServiceException(e.getMessage());
        }
        return projects;
    }

    private boolean isQualifiedProject(ProjectBO project) {
        if (null == project) {
            return false;
        }
        String projectName = project.getProjectName();
        if (StringUtils.isBlank(projectName)) {
            return false;
        }
        String projectEmail = project.getProjectEmail();
        if (StringUtils.isBlank(projectEmail)) {
            return false;
        }
        String projectNum = project.getProjectPhone();
        if (StringUtils.isBlank(projectNum)) {
            return false;
        }
        String projectUser = project.getProjectUser();
        if (StringUtils.isBlank(projectUser)) {
            return false;
        }
        return true;
    }

    @Override
    public List<ProjectBO> getAllProjects() {
        try {
            List<ProjectBO> list = projectDAO.getAllProjects();
            if (list != null && list.size() > 0) {
                return list;
            }
        } catch (Exception e) {
            LOG.error(e.toString());
        }

        return null;
    }

    @Override
    public List<ProjectBO> findAllProjectByCurrentEmail(String email) {
        try {
            List<ProjectBO> list = projectDAO.findProjectByCurrentEmail(email);
            if (list != null && list.size() > 0) {
                return list;
            }
        } catch (Exception e) {
            LOG.error(e.toString());
        }
        return null;
    }

    @Override
    public List<ProjectBO> findProjectByNames(List<String> projectNames) {
        return projectDAO.findProjectByNames(projectNames);
    }
}
